package com.example.administrator.smartbutler.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.example.administrator.smartbutler.MainActivity;
import com.example.administrator.smartbutler.R;
import com.example.administrator.smartbutler.entity.MyUser;
import com.example.administrator.smartbutler.utils.ShareUtils;
import com.example.administrator.smartbutler.utils.StaticClass;

import cn.bmob.v3.BmobUser;

/**
 * 项目名:SmartButler
 * 包名:com.example.administrator.smartbutler.ui
 * 文件名：SplashActivity
 * 创建者：justin
 * Created by Administrator on 2017/12/21.
 * 描述：闪屏页
 */

public class SplashActivity extends AppCompatActivity {

    /**
     * 1.延时2000ms
     * 2.判断程序是否第一次运行
     * 3.自定义字体
     * 4.Activity 全屏主题
     */


    private TextView tv_splash;

    private Handler handler =new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case StaticClass.HANDLER_SPLASH:
                    //判断程序是否是第一次运行
                    if (isFirst()){

                        startActivity(new Intent(SplashActivity.this,GuideActivity.class));

                    }else {

                        BmobUser bmobUser = MyUser.getCurrentUser();
                        if(bmobUser != null){
                            // 允许用户使用应用
                            startActivity(new Intent(SplashActivity.this,MainActivity.class));
                        }else{
                            //缓存用户对象为空时， 可打开用户注册界面…
                            startActivity(new Intent(SplashActivity.this,LoginActivity.class));
                        }

                    }
                    finish();
                    break;

            }

        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splash);

        initView();
    }

    //初始化view
    private void initView() {

        handler.sendEmptyMessageDelayed(StaticClass.HANDLER_SPLASH,2000);

        tv_splash = (TextView) findViewById(R.id.tv_splash);


    }

    //判断程序是否是第一次运行
    private boolean isFirst(){

        boolean isFirst = ShareUtils.getBoolean(this, StaticClass.SHARE_IS_FIRST, true);

        if (isFirst){

            ShareUtils.putBoolean(this,StaticClass.SHARE_IS_FIRST,false);
            return isFirst;

        }

        return isFirst;


    }

    //禁止返回键
    @Override
    public void onBackPressed() {
//        super.onBackPressed();
    }
}
